﻿Public Class UsersForm
    Dim dt As DataTable
    Private Sub ChiPhiForm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        dt = UsersDAO.GetAll()
        dgUser.DataSource = dt        
        dgUser.Columns(1).HeaderText = "Tên đầy đủ"
        dgUser.Columns(2).HeaderText = "Tên người dùng"
        dgUser.Columns(3).HeaderText = "Mật khẩu"
        dgUser.Columns(4).HeaderText = "Quyền Hạn"
        dgUser.Columns(0).Visible = False        
        rbUser.Checked = True
    End Sub

    Private Sub btExit_Click(sender As System.Object, e As System.EventArgs) Handles btExit.Click
        Me.Dispose()
    End Sub

    Private Sub dgChiPhi_CellClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgUser.CellClick
        If dgUser.CurrentRow.Index >= 0 Then
            txtFullname.Text = dgUser.CurrentRow.Cells("fullname").Value.ToString()
            txtUsername.Text = dgUser.CurrentRow.Cells("username").Value.ToString()
            txtPassword.Text = dgUser.CurrentRow.Cells("password").Value.ToString()
            Dim admin As Integer = dgUser.CurrentRow.Cells("QuyenHan").Value
            If admin = 1 Then
                rbAdmin.Checked = True
            Else
                rbUser.Checked = True
            End If
        End If
    End Sub

    Private Sub btThem_Click(sender As System.Object, e As System.EventArgs) Handles btThem.Click        
        Dim u As New User
        With u
            .Fullname = txtFullname.Text
            .Password = txtPassword.Text
            .username = txtUsername.Text
            If rbAdmin.Checked Then
                .QuyenHan = 1
            Else
                .QuyenHan = 2
            End If
        End With
        UsersBUS.Them(dt, u)        
    End Sub

    Private Sub btXoa_Click(sender As System.Object, e As System.EventArgs) Handles btXoa.Click
        If dt.Rows.Count > 0 Then
            dt.Rows(dgUser.CurrentRow.Index).Delete()
            DB_Provider.Write(dt, "Users")
        End If
    End Sub

    Private Sub btSua_Click(sender As System.Object, e As System.EventArgs) Handles btSua.Click
        Dim u As New User
        With u
            .MaUser = Integer.Parse(dgUser.CurrentRow.Cells("MaUser").Value)
            .Fullname = txtFullname.Text
            .Password = txtPassword.Text
            .username = txtUsername.Text
            If rbAdmin.Checked Then
                .QuyenHan = 1
            Else
                .QuyenHan = 2
            End If
        End With
        UsersBUS.Sua(dt, u)        
    End Sub

    Private Sub dgUser_CellFormatting(sender As System.Object, e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles dgUser.CellFormatting
        For Each dr As DataGridViewRow In dgUser.Rows
            If dr.Cells("QuyenHan").Value = 1 Then
                dr.Cells("QuyenHan").Style.Format = "Admin"
            Else
                dr.Cells("QuyenHan").Style.Format = "User"
            End If
        Next
    End Sub
End Class